<?php namespace App\Http\Services;

use App\Exceptions\WechatException;
use Requests;
use Requests_Exception;
use Log;

class BusinessLicense
{
    private $baseUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license';
    private $baseUrlOne = 'https://apis.map.qq.com/ws/geocoder/v1/';
    private $baseUrlTwo = 'https://apis.map.qq.com/ws/distance/v1/';
    private $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';
    private $access_token;
    private $client_id = '8y9ujAPhtRNnjZzuLwpAuF2t';
    private $client_secret = 'ZCwDQ2cvgMW3KlaLtYAntllcA1Rwa6o0';
    private $key = "FM3BZ-VYIEW-YHYRU-R4KXG-Z5RY7-FABOT";


    public function getAccessToken()
    {
        return $this->access_token;
    }

    public function setAccessToken($access_token){
        $this->access_token = $access_token;
    }

    /**
     * 营业执照识别
     * @param $fileContent
     * @return bool|mixed
     * @throws WechatException
     */
    public function businessLicenseIdentify($fileContent)
    {
        $accessTokenInfo = self::getAccessTokenByToken();

        $accessToken = $accessTokenInfo->getAccessToken();

        $image = [
            "image" => $fileContent,
        ];

        $response = null;
        try {
            $response = \Requests::post($this->baseUrl.'?access_token='.$accessToken, [
                'Content-Type' => 'application/x-www-form-urlencoded;',
            ], $image);
        } catch (Requests_Exception $ex) {
            Log::error('Request Error To chepaishibie:' . $ex);
        }

        if (!$response->success) {
            return false;
        }

        $res = json_decode($response->body);
        if (is_null($res))
            return false;

        return $res;
    }

    /**
     * 根据地址获取经纬度
     * @param $address
     * @return bool|mixed
     */
    public static function searchFeedService($address)
    {
        $feed = new BusinessLicense();

        $response = null;
        try {
            $response = \Requests::get($feed->baseUrlOne.'?address='.$address.'&key='.$feed->key,array(), array());
        } catch (Requests_Exception $ex) {
            Log::error('Request Error To chepaishibie:' . $ex);
            return $ex;
        }

        if (!$response->success) {
            return false;
        }

        $res = json_decode($response->body);
        if (is_null($res))
            return false;

        return $res;
    }

    /**
     * 经纬度计算距离
     * @param int $lng
     * @param int $lat
     * @return bool|\Exception|mixed|Requests_Exception
     */
    public static function distanceCalculation($lng1 = 0,$lat1 = 0,$lng2 = 0,$lat2 = 0)
    {
        if(empty($lng1) || empty($lat1) || empty($lng2) || empty($lat2))
            return false;

        $feed = new BusinessLicense();

        $response = null;
        try {
            $response = \Requests::get($feed->baseUrlTwo.'?mode=driving&from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&key='.$feed->key,array(), array());
        } catch (Requests_Exception $ex) {
            Log::error('Request Error To chepaishibie:' . $ex);
            return $ex;
        }

        if (!$response->success) {
            return false;
        }

        $res = json_decode($response->body);
        if (is_null($res))
            return false;

        return $res;
    }

    /**
     * 获取access_token
     * @return BusinessLicense|bool
     * @throws WechatException
     */
    public static function getAccessTokenByToken(){
        $response = null;

        $CarBrandDiscern = new BusinessLicense();

        $url = $CarBrandDiscern->accessTokenUrl.'?grant_type=client_credentials&client_id='.$CarBrandDiscern->client_id.'&client_secret='.$CarBrandDiscern->client_secret;

        try {
            $response = \Requests::post($url, array(), array());
        } catch (Requests_Exception $ex) {
            Log::error('Request Error To chepaishibie:' . $ex);
        }

        if (!$response->success) {
            return false;
        }

        $res = json_decode($response->body);
        if(!empty($res->access_token)) {
            $CarBrandDiscern->setAccessToken($res->access_token);
            return $CarBrandDiscern;
        }else{
            throw new WechatException(WechatException::GetAccessTokenFail);
        }
    }
}
